var lockFlag = false;
var getSmscodeMin = 2;
var remainSecondTipTimer;
var remainCount;

$(function(){

// 获取短信验证码
	$("#btn_sendSmCode").click(function() {
			// 验证是否达到了2分钟的时间
			if (!lockFlag) {
				if (!verifyInputField("phone,img_captcha_fixed"))
					return;
				if (!verifyInputLength("img_captcha_fixed", 4, 4))
					return;
				if(!isPhone("phone"))
					return ;
				// 异步图形码验证
				$.get("captcha/check/pic/" + $("#img_captcha_fixed").val(), {}, function(data) {
							if (!data.result) {
								alertMsgAndFocus(data.message, "#img_captcha_fixed");
								return;
							}
							$(this).val("验证码发送中...").attr("disabled", true);
							// 异步获取短信验证码
							var rurl = "captcha/send/sm/editphone/" +$("#phone").val();
							$.get(rurl, {}, function(data) {
										if (data.result) {
											lockFlag = true;
											remainCount = getSmscodeMin * 60;
											remainSecondTipTimer = setInterval(remainSencodTip, 1000);
											remainSencodTip();
											$("#hid_oprno").val("true");
										} else {
											lockFlag = false;
											$("#btn_sendSmCode").val("重新获取验证码").attr("disabled", false);
										}
										alertMsg(data.message);
									});
						});
			} else {
				alertMsg('"获取短信验证码"点击间隔不能少于' + getSmscodeMin + '分钟！');
			}
		});
});
function nextStep(){
		if (!verifyInputField("phone,img_captcha_fixed,sm_captcha_dynamic"))
			return;
		
		if(!isPhone("phone"))
			return ;
			
		if (!verifyInputLength("img_captcha_fixed", 4, 4))
			return;
			
		if (!verifyInputLength("sm_captcha_dynamic", 6, 6))
			return;
		
		// 异步图形码验证
		$.get("captcha/check/pic/" + $("#img_captcha_fixed").val(), {}, function(data) {
				if (!data.result) {
					alertMsgAndFocus(data.message, "#img_captcha_fixed");
					return;
				}
				// 异步短信码验证
				$.get("captcha/check/sm/4/" + $("#phone").val() + "/" + $("#sm_captcha_dynamic").val(), {}, function(data) {
						if (!data.result) {
							alertMsgAndFocus(data.message, "#sm_captcha_dynamic");
							return;
						}
						// 异步修改手机号码
						$.post("usercenter/safecenter/update_phone",{
								phone : $("#phone").val()
								},function(data){
									if(data.result === true){
										window.location.href=getRootUrl()+'usercenter/safecenter/validate_phone/3'
									}else{
										alertMsg(data.result);
									}
							});
					});
				
			});
	
}

//计算时间
function remainSencodTip() {
	if (remainCount == 0) {
		refreshImgKaptcha("kaptcha_fixed");
		$("#img_captcha_fixed").val('');
		$("#btn_sendSmCode").val("重新获取验证码").attr("disabled", false);
		lockFlag = false;
		clearInterval(remainSecondTipTimer);
		return;
	}
	$("#btn_sendSmCode").val(remainCount + "秒后可重新获取");
	remainCount--;
}

function refreshImgKaptcha(id) {
	$("#" + id)[0].src = "kaptcha.jpg?t=" + Math.random();
}